<?php
class EmailComponent extends CComponent
{
  public function emailHandler($event)
  {
  		$param = $event->params;
  	
  		switch($param['email_slug'])
  		{
  			case 'registration':
				$slug = "registration";
				$user_object = $param['object'];
				$search_values = array('[FIRST_NAME]','[EMAIL]');
				$replace_values = array($user_object->username,$user_object->email);
				$to_email = $user_object->email;				
				$this->sendMailBasedOnSlug($slug, $to_email, $search_values, $replace_values);
  			break;


  			case 'forgot_password':
  				$slug = 'forgot_password';
				$object = $param['object'];
				$search_values = array('[FIRST_NAME]','[EMAIL]', '[FORGOT_PASSWORD_LINK]');
				// create link
				$link = "http://www.test.com/";				
				$replace_values = array($object->username,$object->email, $link);
				$to_email = $object->email;		
				$this->sendMailBasedOnSlug($slug, $to_email, $search_values, $replace_values);  				
  			break;
  			
  			case 'change_password' :
  				$slug = 'change_password';
				$user_object = $param['object'];
				$search_values = array('[FIRST_NAME]','[EMAIL]');
				$replace_values = array($user_object->username,$user_object->email);
				$to_email = $user_object->email;		
				$this->sendMailBasedOnSlug($slug, $to_email, $search_values, $replace_values);  				
  			break;

  			case 'order_placed' :
  			$slug = 'order_placed';
				$order_object = $param['object'];
				$model = Customers::model()->findByPk($order_object->id_customer);
				$restaurant = Restaurant::model()->findByPk($order_object->id_restaurant);
				if($order_object->order_date != "")
					$time = $order_object->order_date ." " .$order_object->order_time;
				else 
					$time = "ASAP";	
				$details = OrderDetails::getItems($order_object->id);				
				$search_values = array('[PAYMENT_MODE]','[FINAL_AMOUNT]','[ORDER_ID]','[NAME]','[RESTAURANT_NAME]','[RESTAURANT_ADDRESS]','[CHANGE]','[INSTRUCTIONS]','[DATE]','[ORDER_DETAILS]','[DEL_CONDITIONS]');
				$replace_values = array($order_object->payment_mode,$order_object->final_amount,$order_object->order_id,$model->firstname,$restaurant->name,Restaurant::model()->getAddress($restaurant->id),$order_object->change,$order_object->secure_key,$time,$details,$restaurant->delivery_conditions);
				$to_email = 'ameyasuti@gmail.com';		
				$this->sendMailBasedOnSlug($slug, $to_email, $search_values, $replace_values);  				
  			break;
  			
  			case 'orderplaced_customer' :
  			$slug = 'orderplaced_customer';
				$order_object = $param['object'];
				$model = Customers::model()->findByPk($order_object->id_customer);
				$restaurant = Restaurant::model()->findByPk($order_object->id_restaurant);
				if($order_object->order_date != "")
					$time = $order_object->order_date ." " .$order_object->order_time;
				else 
					$time = "ASAP";	
				$details = OrderDetails::getItems($order_object->id);				
				$search_values = array('[PAYMENT_MODE]','[FINAL_AMOUNT]','[ORDER_ID]','[NAME]','[RESTAURANT_NAME]','[RESTAURANT_ADDRESS]','[CHANGE]','[INSTRUCTIONS]','[DATE]','[ORDER_DETAILS]','[DEL_CONDITIONS]');
				$replace_values = array($order_object->payment_mode,$order_object->final_amount,$order_object->order_id,$model->firstname,$restaurant->name,Restaurant::model()->getAddress($restaurant->id),$order_object->change,$order_object->secure_key,$time,$details,$restaurant->delivery_conditions);
				$to_email = $model->email_address;
				$this->sendMailBasedOnSlug($slug, $to_email, $search_values, $replace_values);  				
  			break;

  			case 'customer_registration' :
  				$slug = 'customer_registration';
				$order_object = $param['object'];
				$search_values = array('[FIRSTNAME]','[LASTNAME]','[EMAIL_ADDRESS]','[PASSWORD]');
				$replace_values = array($order_object->firstname,$order_object->lastname,$order_object->email_address,$order_object->password);
				$to_email = $order_object->email_address;		
				$this->sendMailBasedOnSlug($slug, $to_email, $search_values, $replace_values);  				
  			break;
  			
  			case 'forwarded' :
  				$slug = 'forwarded';
				$order_object = $param['object'];
        $search_values = array('[STATUS]','[ORDER_ID]');
				$replace_values = array($order_object->status,$order_object->order_id);
				$to_email = 'ameyasuti@gmail.com';
				$this->sendMailBasedOnSlug($slug, $to_email, $search_values, $replace_values);  				
  			break;  			

  		}
  }
  
  public function sendMailBasedOnSlug($slug,$to_email,$search_values,$replace_values)
  {
	  	$condition = "slug=:slug";
		$params = array(':slug' => $slug); 
		
	  	$email_template_obj = EmailTemplate::model()->find($condition,$params);
		
	  	if($email_template_obj != null)
	  	{
		  	$subject = str_replace($search_values,$replace_values,$email_template_obj->subject);
		  	$body = str_replace($search_values,$replace_values,$email_template_obj->html_body);
		  	$from_email = $email_template_obj->from_email;
		  	$from_name = $email_template_obj->from_name;
		  	$this->sendMail($subject,$body,$to_email,$from_email,$from_name);
	  	}
	  	else {
	  		echo "Could not find email template!";
	  	}
  }
  
  public function sendMail($subject,$body,$to_email,$from_email,$from_name,$content_type="text/html",$charset="UTF-8")
  {
  		
  	$body = nl2br($body);
    $headers = 	"From:$from_email" . "\r\n" .
        		"Reply-To: $from_email" . "\r\n" .
        		'X-Mailer: PHP/' . phpversion()."\r\n";
	$headers  .= 'MIME-Version: 1.0' . "\r\n";
	$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
		
	mail($to_email, $subject, $body, $headers); 
    	
  }
  
}